// pages/smsInvitation/smsInvitation.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //历史要约人
    invitaions: [],
    // 参会者名单
    participants: [],
    // 姓名
    name: '',
    // 手机
    phone: '',
    // 标记
    flag: false,
    oldlist:[],
    newlist:[],
    orderinfo:[],
    OrderNo:null,
  },
  // 手机号
  getPhone(e) {
    this.setData({
      phone: e.detail.value
    })
    console.log( e.detail.value)
  },
  //名称
  getName(e) {
    this.setData({
      name: e.detail.value
    })
    console.log( e.detail.value)
  },
  join(e){
    let that = this;
    let name = that.data.name;
    let phone = that.data.phone;
    let item = that.data.participants;
    console.log(name.length);
    if(name.length=='0'){
      wx.showToast({ title: '请填写用户名', icon: 'loading', duration: 1000 })
    }else if(phone.length=='0' || !(/^1[34578]\d{9}$/.test(phone))) {
      wx.showToast({ title: '手机号填写错误', icon: 'loading', duration: 1000 }) 
    }else{
      item.push({
        GuestName:name,
        Mobile:phone
      })
      that.setData({
        participants:item,
        name:'',
        phone:''
      })
      console.log(item)
    }
    
  },
  // 删除历史要约人
  deletData(e) {
    let that =  this
    let item = e.currentTarget.dataset.item;
    let oldlist = that.data.invitaions
    console.log(oldlist[item].Mobile)
    let token = wx.getStorageSync('token')
    //请求删除
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx',
      data: {
        cmd:'DelInviter',
        officeId:1,
        token:token,
        mobile:oldlist[item].Mobile
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: "get",
      success (res) {
        wx.showToast({
          title: res.data.DESC,
          icon: 'success',
          duration: 2000
        })
      oldlist.splice(item,1)
      that.setData({
        invitaions:oldlist
      })
      }
    })
  },

  // 向右添加
  toRight(e) {
    let that =  this
    let item = e.currentTarget.dataset.item;
    let oldlist = that.data.invitaions
    console.log(oldlist[item])
    let participants = that.data.participants;
    participants.push({
      GuestName:oldlist[item].GuestName,
      Mobile:oldlist[item].Mobile
    })
    that.setData({
      participants:participants
    })
   //console.log(participants)
  },
  // 参与者名单删除
  deletJoin(e) {
    let that =  this
    console.log(e.currentTarget.dataset.item);
    let item = e.currentTarget.dataset.item;
    let participants = that.data.participants

    participants.splice(item,1)
    console.log(participants)
    that.setData({
      participants:participants
    })
  },
  //发送短信
  addsms(e){
    let that = this
    let participants = that.data.participants
    let token = wx.getStorageSync('token')
    let OrderNo = that.data.OrderNo
    let data = {
      officeId:1,
      OrderNo:OrderNo,
      Token:token,
      Msg:'',
      GuestList:participants
    }
    let jsondata = JSON.stringify(data)
    console.log(jsondata)
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx?cmd=SendInvite',
      data: jsondata,
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: "post",
      success (res) {
        if(res.data.STATUS=='Success'){
          wx.showToast({
            title: res.data.DESC,
            icon: 'success',
            duration: 2000
          })
          wx.navigateTo({
            url: '../orderInfo/orderInfo',
          })
        }else{
          wx.showToast({
            title: res.data.DESC,
            icon: 'error',
            duration: 2000
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    let OrderNo = options.OrderNo
    let token = wx.getStorageSync('token')
    //请求订单详情
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx',
      data: {
        cmd: 'GetOrderInfo',
        OrderNo: OrderNo,
        token:token,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        that.setData({
          orderinfo:res.data.DATA[0]
        })
      }
    })
   //请求历史邀请
    that.setData({
      OrderNo:OrderNo,
    })
    //获取历史邀请人
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx', //仅为示例，并非真实的接口地址
      data: {
        cmd: 'getInviters',
        yofficeId:1,
        token:token
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        that.setData({
          invitaions:res.data.DATA
        })
      }
    })



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  navigateBack(){
    wx.navigateBack();
  }
  
})